package com.googlecode.wicketcharts.highcharts.gson;

import java.lang.reflect.Type;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.googlecode.wicketcharts.highcharts.options.Center;
import com.googlecode.wicketcharts.highcharts.options.Center.Unit;

/**
 * Serializer for the {@link Center} class.
 * 
 * @author Tom Hombergs (tom.hombergs@gmail.com)
 * 
 */
public class GsonCenterSerializer implements JsonSerializer<Center> {

	@Override
	public JsonElement serialize(Center src, Type typeOfSrc,
			JsonSerializationContext context) {
		JsonArray array = new JsonArray();

		if (src.getUnit() == Unit.PERCENT) {
			String x = String.valueOf(src.getX()) + "%";
			String y = String.valueOf(src.getY()) + "%";
			array.add(new JsonPrimitive(x));
			array.add(new JsonPrimitive(y));
		}else{
			array.add(new JsonPrimitive(src.getX()));
			array.add(new JsonPrimitive(src.getY()));
		}

		return array;
	}
}
